import jwt
from fastapi import Depends, Header, HTTPException
from sqlalchemy.orm import Session

from core.database import get_db


def common_dependencies(db: Session = Depends(get_db), token: str = Header(None)):
    if token is None:
        raise HTTPException(status_code=401, detail="Token missing")
    try:
        payload = jwt.decode(token, "demo-key", algorithms='HS256')
        return {"db": db, "user_id": payload.get("user_id"), "username": payload.get("username")}
    except jwt.ExpiredSignatureError:
        raise HTTPException(status_code=401, detail="Token invalid")
